﻿
#include <QApplication>
#include <QMenu>
#include <QProcess>
#include <QMessageBox>
#include <QSystemTrayIcon>
#include <unistd.h>

#include "appcommon.h"
#include "mainwindow.h"
#include "windowlogin.h"
#include "config/appconfig.h"
#include "process/singleton_app.h"


int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    SingletonApp sapp("ep_vpn_client_tool");
    // 检查，不允许第2次启动
    if(sapp.isRunning()){
        QMessageBox::warning(nullptr,"提示","程序[ep_vpn_client_tool]已经启动，请手动检查");
        return 0;
    }


    a.setWindowIcon(QIcon("://qrc/vpn.png"));


    // 创建系统托盘图标
    QSystemTrayIcon *trayIcon = new QSystemTrayIcon();
    trayIcon->setIcon(QIcon("://qrc/vpn.png"));
    trayIcon->setToolTip("SoftEther客户端助手");

    AppConfig::instance().loadConfig();

    WindowLogin w;

    // 创建右键菜单
    QMenu *trayMenu = new QMenu();
    trayMenu->addAction("显示", [&w]() {
        w.showForm();
    });
    trayMenu->addAction("退出程序", [&w]() {
        w.closeForm();
    });

    // 关联菜单到托盘图标
    trayIcon->setContextMenu(trayMenu);

    // 显示托盘图标
    trayIcon->show();

    AppCommon::showWindowToCenter(&w);

    int ret = a.exec();
    w.cleanVpn();
    delete trayIcon;
    return ret;
}
